package model

import (
	"time"

	"gorm.io/gorm"
)

type User struct {
	UserID           uint      `gorm:"primaryKey;column:user_id;autoIncrement" json:"user_id"`
	Phone            string    `gorm:"unique;type:varchar(11);not null" json:"phone"`
	RemainingPoints  int       `gorm:"default:0" json:"remaining_points"`
	RegistrationTime time.Time `gorm:"type:datetime;default:CURRENT_TIMESTAMP" json:"registration_time"`
	IsDeleted        bool      `gorm:"column:is_deleted;default:false;comment:是否已软删除"`
}

func (User) TableName() string {
	return "user"
}

// 关联积分订单（需PointsOrder模型存在）
func (u *User) PointsOrders(db *gorm.DB) ([]PointsOrder, error) {
	var orders []PointsOrder
	if err := db.Where("user_id = ?", u.UserID).Find(&orders).Error; err != nil {
		return nil, err
	}
	return orders, nil
}
